<template>
  <div>
    <el-divider content-position="center">工况点性能参数</el-divider>
    <div style="
        display: flex;
        justify-content: flex-end;
        margin-bottom: 20px;
        margin-right: 20px;
      ">
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddatmosphere">添加</el-button>
    </div>
    <el-table :data="ttaskPerformancePointList" :row-class-name="rowDetailIndex" ref="tProductFailureDetail"
      max-height="300" border>
      <el-table-column label="序号" align="center" prop="index" width="50" />
      <el-table-column label="工况点" align="center" prop="performanceName" min-width="150" show-overflow-tooltip />
      <el-table-column label="工作转速 r/min" align="center" prop="workSpeed" min-width="150" show-overflow-tooltip />
      <el-table-column label="静叶角度 °" align="center" prop="guideVaneAngle" min-width="150" show-overflow-tooltip />
      <el-table-column label="多变效率 %" align="center" prop="polyEfficiency" min-width="150" show-overflow-tooltip />
      <el-table-column label="内功率 kW" align="center" prop="internalPower" min-width="150" show-overflow-tooltip />

      <el-table-column label="排气温度K" align="center" prop="outletTempK" min-width="150" show-overflow-tooltip />
      <el-table-column label="排气温度℃" align="center" prop="outletTempC" min-width="150" show-overflow-tooltip />

      <el-table-column label="轴功率 kW" align="center" prop="shaftPower" min-width="150" show-overflow-tooltip />

      <el-table-column label="轴功率(修正) kW" align="center" prop="shaftPowerCorrected" min-width="150"
        show-overflow-tooltip />


      <el-table-column label="内功率(修正) kW" align="center" prop="internalPowerCorrected" min-width="150"
        show-overflow-tooltip />
      <el-table-column label="出口温度(修正) ℃" align="center" prop="outletTempCorrected" min-width="150"
        show-overflow-tooltip />

      <el-table-column label="多变效率(修正) %" align="center" prop="polyEfficiencyCorrected" min-width="150"
        show-overflow-tooltip />
      <el-table-column label="多变效率修正量 %" align="center" prop="polyEfficiencyDelta" min-width="150"
        show-overflow-tooltip />
      <el-table-column label="等熵效率 %" align="center" prop="isentropicEfficiency" min-width="150"
        show-overflow-tooltip />
      <el-table-column label="多变能量头 kJ/kg" align="center" prop="polyEnergyHead" min-width="150" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdateatmosphere(scope.row)">修改</el-button>
          <el-button size="mini" type="text" @click="handleDeleteatmosphere(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="工况点性能参数" :visible.sync="Open" :close-on-click-modal="false" width="800px" append-to-body
      v-dialogDrag>
      <el-form ref="forms" :model="forms" label-width="125px" :inline="true" class="addData">
        <el-form-item label="工况点" prop="performanceName">
          <el-input v-model="forms.performanceName" placeholder="请输入工况点" />
        </el-form-item>
        <el-form-item label="工作转速 r/min" prop="workSpeed">
          <el-input v-model="forms.workSpeed" placeholder="请输入工作转速 r/min" />
        </el-form-item>
        <el-form-item label="静叶角度 °" prop="guideVaneAngle">
          <el-input v-model="forms.guideVaneAngle" placeholder="请输入静叶角度 °" />
        </el-form-item>
        <el-form-item label="多变效率 %" prop="polyEfficiency">
          <el-input v-model="forms.polyEfficiency" placeholder="请输入多变效率" />
        </el-form-item>
        <el-form-item label="内功率 kW" prop="internalPower">
          <el-input v-model="forms.internalPower" placeholder="请输入内功率" />
        </el-form-item>
        <el-form-item label="排气温度K" prop="outletTempK">
          <el-input v-model="forms.outletTempK" placeholder="请输入排气温度K" />
        </el-form-item>
        <el-form-item label="排气温度℃" prop="outletTempC">
          <el-input v-model="forms.outletTempC" placeholder="请输入排气温度" />
        </el-form-item>
        <el-form-item label="轴功率 kW" prop="shaftPower">
          <el-input v-model="forms.shaftPower" placeholder="请输入轴功率 kW" />
        </el-form-item>
        <el-form-item label="轴功率(修正) kW" prop="shaftPowerCorrected">
          <el-input v-model="forms.shaftPowerCorrected" placeholder="请输入轴功率" />
        </el-form-item>
        <el-form-item label="内功率(修正) kW" prop="internalPowerCorrected">
          <el-input v-model="forms.internalPowerCorrected" placeholder="请输入内功率" />
        </el-form-item>
        <el-form-item label="出口温度(修正) ℃" prop="outletTempCorrected">
          <el-input v-model="forms.outletTempCorrected" placeholder="请输入出口温度(修正)" />
        </el-form-item>
        <el-form-item label="多变效率(修正) %" prop="polyEfficiencyDelta">
          <el-input v-model="forms.polyEfficiencyDelta" placeholder="请输入多变效率修正量 %" />
        </el-form-item>
        <el-form-item label="多变效率修正量 %" prop="polyEfficiencyCorrected">
          <el-input v-model="forms.polyEfficiencyCorrected" placeholder="请输入多变效率" />
        </el-form-item>
        <el-form-item label="等熵效率 %" prop="isentropicEfficiency">
          <el-input v-model="forms.isentropicEfficiency" placeholder="请输入等熵效率 %" />
        </el-form-item>
        <el-form-item label="多变能量头kJ/kg" prop="polyEnergyHead">
          <el-input v-model="forms.polyEnergyHead" placeholder="请输入多变能量头 kJ/kg" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormatmosphere">确 定</el-button>
        <el-button @click="cancelatmosphere">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      currentEditIndex: -1,
      ttaskPerformancePointList: [],
      Open: false,
      forms: {},
    };
  },
  props: {
    formData: {
      type: Array,
      default: () => [],
    },
  },
  watch: {
    formData: {
      immediate: true,
      deep: true,
      handler(newVal) {
        // 更新组件内部数据
        this.ttaskPerformancePointList = JSON.parse(JSON.stringify(newVal));
      },
    },
  },
  methods: {
    rowDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    handleAddatmosphere() {
      this.Open = true;
      this.currentEditIndex = -1;
      this.resetform();
    },
    // 修改
    handleUpdateatmosphere(row) {
      this.Open = true;
      this.forms = { ...row };

      // 查找当前编辑项的索引
      if (row.id) {
        this.currentEditIndex = this.ttaskPerformancePointList.findIndex(
          (item) => item.id === row.id
        );
      } else {
        this.currentEditIndex = this.ttaskPerformancePointList.findIndex(
          (item) => this.isSameRow(item, row)
        );
      }
    },

    // 删除
    handleDeleteatmosphere(row) {
      this.$confirm("确认删除工况点性能参数数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (row.id) {
            // 根据ID删除
            const index = this.ttaskPerformancePointList.findIndex(
              (item) => item.id === row.id
            );
            if (index !== -1) {
              this.ttaskPerformancePointList.splice(index, 1);
            }
          } else {
            // 没有ID，根据索引删除
            const index = this.ttaskPerformancePointList.findIndex((item) =>
              this.isSameRow(item, row)
            );
            if (index !== -1) {
              this.ttaskPerformancePointList.splice(index, 1);
            }
          }
          this.$message.success("删除成功!");
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    resetform() {
      this.forms = {
        angle: undefined,
        dischargePressure: undefined,
      };
    },
    // 生成临时ID
    generateTempId() {
      return "temp_" + Date.now() + Math.floor(Math.random() * 1000);
    },
    // 判断两行数据是否相同
    isSameRow(row1, row2) {
      const keys = Object.keys(row1);
      return keys.every((key) => row1[key] === row2[key]);
    },
    submitFormatmosphere() {
      this.$refs.forms.validate((valid) => {
        if (valid) {
          const formData = { ...this.forms };
          if (!this.ttaskPerformancePointList) {
            this.ttaskPerformancePointList = [];
          }
          if (this.currentEditIndex === -1) {
            // 新增操作
            if (!formData.id) {
              formData.temp_id = this.generateTempId(); // 为新增数据生成临时ID
            }

            this.ttaskPerformancePointList.push(formData);
          } else {
            // 修改操作
            if (formData.id) {
              // 根据ID修改
              const index = this.ttaskPerformancePointList.findIndex(
                (item) => item.id === formData.id
              );
              if (index !== -1) {
                this.ttaskPerformancePointList.splice(index, 1, formData);
              }
            } else {
              // 没有ID，根据行数据匹配修改
              this.ttaskPerformancePointList.splice(
                this.currentEditIndex,
                1,
                formData
              );
            }
          }
          this.$emit(
            "getTtaskPerformancePointList",
            this.ttaskPerformancePointList
          );
          this.Open = false;
          this.resetform();
        }
      });
    },
    cancelatmosphere() {
      this.Open = false;
      this.currentEditIndex = -1;
      this.resetform();
    },
  },
};
</script>
<style scoped lang="scss"></style>
